/**
 * Created by IAN on 2016/10/14.
 */
$(function () {
    //select 加载
    var buildSel = $.Params.create();
    buildSel.ajax("get", getMeetIP() + "build", function (data) {
        var html = "<option value='0'>请选择会议厅</option>";
        if(data.count > 0){
            $.each(data.rows,function(i,val){
                html+="<option value = '"+val.id+"'>"+val.buildTitle+"</option>";
            })
        }
       $("#buildTitle").html(html);
    })

    $("#buildTitle").on("change",function(){
        var buildId = $(this).val();
        getSubject(buildId);
    })


    function getSubject(buildId){
        var subjectSel = $.Params.create();
        subjectSel.addParams("buildId",buildId);
        subjectSel.ajax("get", getMeetIP() + "meetSubject", function (data) {
            var html = "<option value='0'>请选择专题</option>";
            if(data.count > 0){
                $.each(data.rows,function(i,val){
                    html+="<option value = '"+val.id+"'>"+val.subjectTitle+"("+val.meetDate+")</option>";
                })
            }
           $("#subjectTitle").html(html);
        })
    }


    var sel = $.Params.create();
    sel.addParams("page", 0);
    sel.addParams("rows", rows);
    getData();

    //查询
    $("#searchBtn").on("click", function () {
        var buildId = $("#buildTitle").val();
        var meetSubjectId = $("#subjectTitle").val();
        var type = $("#type").val();
        var title = $("#title").val();
        if(buildId != "0"){
            sel.addParams("buildId", buildId);
        }else{
            sel.addParams("buildId", undefined);
        }
        if(meetSubjectId != "0"){
            sel.addParams("meetSubjectId", meetSubjectId);
        }else{
            sel.addParams("meetSubjectId", undefined);
        }
        if(type != "0"){
            sel.addParams("type", type);
        }else{
            sel.addParams("type", undefined);
        }
        if (title != "") {
            sel.addParams("scheduleTitle", title);
        } else {
            sel.addParams("scheduleTitle", undefined);
        }
        getData();
    })

    //判断checkbox状态
    $("#selAll").on("click", function () {
        if (!$(this).attr("checked")) {
            $("input[type=checkbox]").attr("checked", "checked")
        } else {
            $("input[type=checkbox]").removeAttr("checked");
        }
    })

    //添加事件
    $('#addDataBtn').on("click", function () {
        window.location.href = "meetScheduleAdd.html";
    });

    //删除事件
    $('#delBtn').on("click", function () {
        var params = getCheckData();
        if (params != "") {
            if (confirm("确定要删除吗！")) {
                var del = $.Params.create();
                //del.addParams("params", params)
                del.ajax("delete", getMeetIP() + "meetSchedule?params=" + params, function (data) {
                    
                        alert("删除成功！");
                        getData();
                    
                })
            }
        } else {
            alert("请至少选中一项");
        }
    });

    //获取数据
    function getData() {
        getPageData("get", sel, getMeetIP() + "meetSchedule", function (data) {
                var html = "";
                if (data.count > 0) {
                    $.each(data.rows, function (i, val) {
                        var type = "";
                        if(val.type == 1){
                            type="讲者日程";
                        }else if(val.type == 2){
                            type = "主持日程";
                        }
                        html += "<tr role='row'' class='even'>" +
                            "<td><input eid='" + val.id + "' onclick='getCheck(this)' type='checkbox' class='classOption'></td>" +
                            "<td>" + val.Build.buildTitle + "</td>" +
                            "<td>" + val.MeetSubject.subjectTitle + "</td>" +
                            "<td>" + val.scheduleTitle + "</td>" +
                            "<td>" + val.name + "</td>" +
                            "<td>" + val.startTime + "</td>" +
                            "<td>" + val.endTime + "</td>" +
                            "<td>" + val.meetTime + "</td>" +
                            "<td>" + val.meetDate + "</td>" +
                            "<td>" + type+ "</td>" +
                            "<td><button type='button' class='btn btn-primary'  onclick='updateInfo(\"" + val.id + "\")'>修改</button></td>" +
                            "</tr>";
                    });

                } else {
                    html = "<tr><td>暂无数据</td></tr>"
                }
                $("#listContainer").html(html);
        })
    }
    updateInfo = function (eid) {
        window.location.href = "meetScheduleUpdate.html?eid=" + eid;
    }
});

